package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.BulletinAddDTO;
import com.oa.admin.dto.BulletinListDTO;
import com.oa.admin.entity.Bulletin;
import com.oa.admin.service.IBulletinService;
import com.oa.admin.vo.BulletinListVO;
import com.oa.system.entity.User;
import com.oa.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.Date;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/bulletin")
@Api(description = "公告管理模块")
public class BulletinController extends BaseController {

    private final IBulletinService bulletinService;

    @Autowired
    public BulletinController(IBulletinService bulletinService) {
        this.bulletinService = bulletinService;
    }

    @Autowired
    private IUserService userService;

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("/list")
    @CommonController
    @Description("列表")
    @ApiOperation(value = "列表")
    public Resp<List<Bulletin>> findList(BulletinListDTO bulletinListVO) {
        return new Resp().success(bulletinService.findBulletinList(bulletinListVO));
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp<Bulletin> findById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(bulletinService.findBulletinListDetails(id));
    }


    /**
     * 新增
     *
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "公告管理对象") @RequestBody(required = false) BulletinAddDTO bulletinAddDTO) {
        User user = userService.selectById(getLoginUserId());
        bulletinAddDTO.setCreaterId(user.getId());
        bulletinAddDTO.setCreateTime(new Date());
        bulletinAddDTO.setDepartmentId(user.getDepartmentId());
        return bulletinService.add(bulletinAddDTO);
    }

    /**
     * 根据Id修改
     *
     * @return Resp
     */
    @PutMapping("")
    @CommonController
    @Description("根据id修改")
    @ApiOperation(value = "根据id修改")
    public Resp update(@ApiParam(value = "公告管理对象") @RequestBody(required = false) BulletinAddDTO bulletinAddDTO) {
        return bulletinService.edit(bulletinAddDTO);
    }

    /**
     * 根据Id删除
     *
     * @param id id
     * @return Resp
     */
    @DeleteMapping("/{id}")
    @CommonController
    @Description("根据id删除")
    @ApiOperation(value = "根据id删除")
    public Resp delete(@ApiParam(value = "id", required = true) @PathVariable("id") String id) {
        Bulletin bulletin = bulletinService.selectById(id);
        if (bulletin != null && bulletin.getIsDeleted() == 2) {
            bulletin.setIsDeleted(1);
            if (bulletinService.updateById(bulletin)) {
                return new Resp().success();
            }
            return new Resp().failure();
        }
        return new Resp().failure();
    }
}
